﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;

namespace OfficeToolbox.SharePoint.Lists.FormSettings {
    public class FormSettingsPage : LayoutsPageBase {
        // Fields
        private StringBuilder computeFieldsScript = new StringBuilder();
        private SPList currentList;
        private Dictionary<string, Dictionary<string, string>> fieldProperties;
        private Dictionary<string, Dictionary<string, string>> hiddenFields = new Dictionary<string, Dictionary<string, string>>();
        protected Button OK = new Button();
        protected Button Cancel = new Button();

        protected HtmlInputHidden HdnSelectedColsDisplay;
        //protected HtmlInputHidden HdnSelectedColsEditRead;
        protected HtmlInputHidden HdnSelectedColsEdit;
        protected HtmlInputHidden HdnSelectedColsNew;
        protected HtmlInputHidden HdnColNames;

        String[] cols;
        String[] colsDisplay;
        String[] colsEdit;
        //String[] colsEditRead;
        String[] colsNew;

        // Methods

        private Dictionary<String, Dictionary<String,String>> GetColumnSettings() {
            Dictionary<String, Dictionary<String, String>> fieldVal = new Dictionary<string, Dictionary<string, string>>();

            cols = HdnColNames.Value.Substring(0, HdnColNames.Value.Length-1).Split(new char[] { ',' });
            colsDisplay = GetArr(HdnSelectedColsDisplay.Value);
            colsEdit = GetArr( HdnSelectedColsEdit.Value);
            //colsEditRead = GetArr(HdnSelectedColsEditRead.Value);
            colsNew = GetArr(HdnSelectedColsNew.Value);

            for (int i = 0; i < cols.Length; i++) {
                fieldVal.Add(cols[i], new Dictionary<string, string>());
                fieldVal[cols[i]].Add("New", colsNew[i]);
                fieldVal[cols[i]].Add("Display", colsDisplay[i]);
                fieldVal[cols[i]].Add("Edit", colsEdit[i]);
                //fieldVal[cols[i]].Add("EditRead", colsEditRead[i]);
            }

            return fieldVal;
        }

        private string[] GetArr(string array) {
            String[] sa = new string[array.Length];
            for(int i=0; i<array.Length; i++)
            {
                sa[i] = array[i].ToString();
            }
            return sa;
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            try {
                Guid uniqueId = new Guid(base.Request.QueryString.GetValues("List")[0]);
                if (!base.IsPostBack) {

                }
                else
                    return;
            }
            catch (Exception) {
                string name = "MissingRequiredQueryString";
                throw new SPException(SPResource.GetString(name, new object[] { "List" }));
            }
            base.Title = "Form Settings";
            if (this.CurrentList.RootFolder.Properties.ContainsKey((object)"ListFormSettings")) {
                this.fieldProperties = FormFieldIterator.ConvertFromString((string)this.CurrentList.RootFolder.Properties[(object)"ListFormSettings"]);
            }
            foreach (SPField field in this.CurrentList.Fields) {
                if (field.Reorderable) {
                }
            }
            this.Cancel.PostBackUrl = string.Format(base.Web.Url + "/_layouts/listedit.aspx?List={0}", this.CurrentList.ID.ToString());
        }

        protected bool IsChecked(SPField field, String mode) {
            try {
                switch (this.fieldProperties[field.InternalName][mode]) {
                    case "1": return true;
                    case "0": return false;
                    default: return true;
                }
            }
            catch { }

                return true;
        }

        protected void ApplySettings(object sender, EventArgs e) {
            this.fieldProperties = GetColumnSettings();

            if (!this.CurrentList.RootFolder.Properties.ContainsKey((object)string.Format("ListFormSettings{0}", this.CurrentList.ID.ToString()))) {
                this.CurrentList.RootFolder.Properties.Add((object)"ListFormSettings", (object)FormFieldIterator.ConvertToString(this.fieldProperties));
            }
            else {
                this.CurrentList.RootFolder.Properties[(object)"ListFormSettings"] = FormFieldIterator.ConvertToString(this.fieldProperties);
            }
            this.CurrentList.RootFolder.Update();
            base.Response.Redirect(string.Format(base.Web.Url + "/_layouts/listedit.aspx?List={0}", this.CurrentList.ID.ToString()), true);
        }

        protected SPList CurrentList {
            get {
                if (this.currentList == null) {
                    this.currentList = SPContext.Current.Web.Lists[new Guid(base.Request.QueryString["List"])];
                }
                return this.currentList;
            }
        }
    }
}